/*
 * @lc app=leetcode.cn id=543 lang=typescript
 *
 * [543] 二叉树的直径
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */
/* TODO 
 并不一定需要穿过根节点，应该遍历每个节点找到左右树最深度 
*/

var diameterOfBinaryTree = function (root: TreeNode | null) {
    let res = 0;
    const deep = (root: TreeNode | null): number => {
        if (!root) return 0;
        let left = deep(root.left);
        let right = deep(root.right);
        res = Math.max(left + right, res);
        // 这里需要加一是因为 算上自身
        return 1 + Math.max(left, right);
    };
    deep(root);
    return res;
};

// @lc code=end
